home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / Gnuplot 3.5 for Macintosh / SOURCES 3.5 / Mac.c < prev    next >
C/C++ Source or Header  |  1993-11-12  |  6KB  |  326 lines

  1. #include "Mac.h"
  2. #include <MacHeaders> 
  3. #include <console.h>
  4.  
  5. //private variables
  6. static     WindowPtr    itsWindow    =NULL;
  7. static    GrafPtr     savePort    =NULL;
  8. static     PicHandle     itsPicture    =NULL;
  9. static Handle saveMBar, myMBar;
  10.  
  11. //private functions prototype
  12. static Boolean MAC_event(void);
  13. static void SetUpMenu(void);
  14. static void AdjustMenu(void);
  15. static void RestoreMenu(void);
  16.  
  17. MAC_init()
  18. {
  19.     Rect    boundsRect = {50, 10, 200, 200};
  20.     
  21.     itsWindow = NewWindow(NULL, &boundsRect,MAC_WINDOW_TITLE, FALSE, 
  22.                             noGrowDocProc, (void *)-1L, FALSE, 0L);
  23.     SizeWindow (itsWindow,MAC_XMAX+40,MAC_YMAX+20,FALSE);
  24.     
  25.     GetPort(&savePort);    
  26.     SetPort(itsWindow);
  27.     TextFont(monaco);
  28.     TextSize(9);
  29.     TextFace(normal);
  30.     PenNormal();
  31.     SetUpMenu();
  32.     SetPort(savePort);
  33. }
  34.  
  35.  
  36. MAC_graphics()
  37. {    
  38.     
  39.     GetPort(&savePort);
  40.     SetPort(itsWindow);
  41.      EraseRect(&(((GrafPtr) itsWindow)->portRect));
  42.     ShowWindow (itsWindow);
  43.     BringToFront (itsWindow);
  44.     HiliteWindow (itsWindow,TRUE);
  45.     if(itsPicture != NULL){
  46.         KillPicture(itsPicture);
  47.         itsPicture=NULL;
  48.     }
  49.     itsPicture=OpenPicture (&(((GrafPtr) itsWindow)->portRect));
  50.     TextFont(monaco);
  51.     TextSize(9);
  52.     TextFace(normal);
  53.     PenNormal();
  54. }
  55.  
  56.  
  57. MAC_text()
  58. {
  59.     Rect myRect;
  60.     
  61.     ClosePicture();
  62.     if(itsPicture != NULL){
  63.         myRect=itsWindow->portRect;
  64.         DrawPicture(itsPicture,&myRect);
  65.     }
  66.     if(itsWindow !=NULL){
  67.         SetWTitle(itsWindow, "\pHit any key to continue.");
  68.           while(!MAC_event());
  69.         SetWTitle(itsWindow, MAC_WINDOW_TITLE);
  70.     }
  71.     SetPort(savePort);
  72.     cshow(stdout);
  73. }
  74.  
  75.  
  76. MAC_linetype(linetype)
  77. int linetype;
  78. {
  79.     int lt;
  80.         
  81.     lt=(linetype > 1?linetype:1);
  82.     PenSize(lt,lt);
  83. }
  84.  
  85.  
  86. MAC_move(x,y)
  87. unsigned int x,y;
  88. {
  89.     MoveTo(x+MAC_XOFFSET,MAC_YMAX-y);
  90. }
  91.  
  92.  
  93. MAC_vector(x,y)
  94. unsigned int x,y;
  95. {
  96.     LineTo(x+MAC_XOFFSET,MAC_YMAX-y);
  97. }
  98.  
  99.  
  100. MAC_put_text(x,y,str)
  101. unsigned int x,y;
  102. char str[];
  103. {
  104.     MoveTo(x+MAC_XOFFSET,MAC_YMAX-y+MAC_VCHAR/2);
  105.     PenNormal();
  106.     DrawString(CtoPstr(str));
  107.     PtoCstr(str);
  108. }
  109.  
  110. MAC_reset()
  111. {
  112.     //GetPort(&savePort);    
  113.     //SetPort(itsWindow);
  114.      //EraseRect(&(((GrafPtr) itsWindow)->portRect));
  115.     //SetPort(savePort);    
  116. }
  117.  
  118. //private functions
  119.  
  120. #define appleM            1
  121. #define fileM            2
  122. #define editM            3
  123.  
  124. /* Edit menu command indices */
  125. #define undoCommand     1
  126. #define cutCommand        3
  127. #define copyCommand        4
  128. #define pasteCommand    5
  129. #define clearCommand    6
  130.  
  131. #define fmNew        1
  132. #define fmOpen        2
  133. #define fmClose        4
  134. #define fmSave        5
  135.  
  136. static Boolean MAC_event(){
  137.     EventRecord    theEvent;
  138.     WindowPtr    whichWindow;
  139.     
  140.     SystemTask ();
  141.     
  142.     if (GetNextEvent (everyEvent, &theEvent))
  143.       switch (theEvent.what){
  144.         case mouseDown:
  145.             switch(FindWindow(theEvent.where,&whichWindow)){
  146.                 case inSysWindow:
  147.                     SystemClick(&theEvent,whichWindow);
  148.                     break;
  149.                 case inDrag:{
  150.                         Rect dragRect;
  151.                         SetRect(&dragRect,4,24,
  152.                             screenBits.bounds.right-4,screenBits.bounds.bottom-4);
  153.                         DragWindow (whichWindow,theEvent.where, &dragRect);
  154.                     }
  155.                     break;
  156.                 case inMenuBar:
  157.                     AdjustMenu();
  158.                     doMenuCommand(MenuSelect(theEvent.where));
  159.                     RestoreMenu();
  160.                     break;
  161.             };
  162.             break;
  163.         case keyDown:
  164.         case autoKey:
  165.             if ((theEvent.modifiers & cmdKey) != 0){
  166.                 AdjustMenu();
  167.                   doMenuCommand(MenuKey(theEvent.message & charCodeMask));
  168.                 RestoreMenu();
  169.             }
  170.             else
  171.                 return TRUE;
  172.             break;
  173.             
  174.         case updateEvt:
  175.             BeginUpdate(itsWindow);
  176.             if(itsPicture != NULL) DrawPicture(itsPicture,&(itsWindow->portRect));
  177.             EndUpdate(itsWindow);
  178.             break;
  179.             
  180.         case activateEvt:
  181.             InvalRect(&itsWindow->portRect);
  182.             break;
  183.         }
  184.         return FALSE;
  185. }
  186.     
  187. static  doMenuCommand(long mCmd){
  188.     int     item,menu;
  189.     Str255        daName;
  190.     short        daRefNum;
  191.  
  192.     item=LoWord(mCmd);
  193.     menu=HiWord(mCmd);
  194.     switch(menu){
  195.         case appleM:
  196.             switch ( item ) {
  197. /*                 case iAbout:        /* bring up alert for About */
  198. /*                    break;            /*                            */
  199.                 default:            /* all non-About items in this menu are DAs */
  200.                     /* type Str255 is an array in MPW 3 */
  201.                     GetItem(GetMHandle(appleM), item, daName);
  202.                     daRefNum = OpenDeskAcc(daName);
  203.                     break;
  204.             }
  205.             break;
  206.         case fileM:
  207.             switch(item){
  208.                 case fmSave:
  209.                     doSave();
  210.                     break;
  211.             }
  212.             break;
  213.         case editM:
  214.             switch(item){
  215.                 case copyCommand:
  216.                     doCopy();
  217.                     break;
  218.             }
  219.             break;
  220.     }
  221.     HiliteMenu(0);
  222. }
  223.  
  224. static doCopy(){
  225.     if( ZeroScrap() != noErr) {
  226.         fprintf(stderr, "Cann't initialize Clippboard\n");
  227. #ifdef THINK_C
  228.         exit(0);
  229. #else
  230.         exit();
  231. #endif
  232.     }
  233.     else{
  234.         HLock(itsPicture);
  235.         PutScrap(GetHandleSize(itsPicture),'PICT', *itsPicture);
  236.         HUnlock(itsPicture);
  237.     }
  238. }
  239.  
  240. // routines to create PICT file.
  241. #define NIL NULL
  242. long PICTCount;
  243. int globalRef;
  244. PicHandle newPICTHand;
  245.  
  246. pascal void PutPICTData(Ptr dataPtr, int byteCount){
  247.     long longCount;
  248.     int    err;
  249.     
  250.     longCount=byteCount;
  251.     PICTCount+=byteCount;
  252.     
  253.     err=FSWrite(globalRef, &longCount, dataPtr);
  254.     if(newPICTHand != NIL) (**newPICTHand).picSize=PICTCount;
  255. }
  256.  
  257. static doSave(){
  258.     Point where={97,103};
  259.     SFReply reply;
  260.     OSErr    err;
  261.     int i;
  262.     long longCount,longZero;
  263.     Rect pFrame;
  264.     QDProcs myProcs;
  265.             
  266.     SFPutFile(where,"\pSave picture into", "\pUntitled", NULL, &reply);
  267.     if(reply.good) {
  268.         err=Create(reply.fName,reply.vRefNum,'????','PICT');
  269.         if( err == noErr || err == dupFNErr) {
  270.             FSOpen(reply.fName,reply.vRefNum,&globalRef);
  271.             SetStdProcs(&myProcs);
  272.             itsWindow->grafProcs=&myProcs;
  273.             myProcs.putPicProc=(Ptr) PutPICTData;
  274.             longZero=0L;
  275.             longCount=4;
  276.             PICTCount=sizeof(Picture);
  277.             for(i=1;i<=(512/4+sizeof(Picture));i++){
  278.                 FSWrite(globalRef,&longCount,&longZero);
  279.             }
  280.             pFrame=(**itsPicture).picFrame;
  281.             newPICTHand=NIL;
  282.             newPICTHand=OpenPicture(&pFrame);
  283.             DrawPicture(itsPicture,&pFrame);
  284.             ClosePicture();
  285.             SetFPos(globalRef, fsFromStart,512);
  286.             longCount=sizeof(Picture);
  287.             FSWrite(globalRef,&longCount,(Ptr) (*newPICTHand));
  288.             FSClose(globalRef);
  289.             itsWindow->grafProcs=NIL;
  290.             KillPicture(newPICTHand);
  291.         }
  292.         else{
  293.         /* handle error */
  294.         }
  295.     }
  296. }
  297.  
  298. static void SetUpMenu(void){
  299.     MenuHandle mh;
  300.     
  301.     saveMBar=GetMenuBar();
  302.     ClearMenuBar();
  303.     mh=NewMenu(appleM,"\p\024");
  304.     InsertMenu(mh,0);
  305.     AddResMenu(GetMHandle(appleM), 'DRVR');    /* add DA names to Apple menu */
  306.     mh=NewMenu(fileM,"\pFile");
  307.     AppendMenu(mh, "\p(New/N;(Open/O;(-;(Close/W;Save/S");
  308.     InsertMenu(mh,0);
  309.     mh=NewMenu(editM,"\pEdit");
  310.     AppendMenu(mh, "\p(Undo/Z;(-;(Cut/X;Copy/C");
  311.     InsertMenu(mh,0);
  312.     DrawMenuBar();
  313.     myMBar=GetMenuBar();
  314. }
  315.  
  316. static void AdjustMenu(void){
  317.     SetMenuBar(myMBar);
  318.     DrawMenuBar();
  319. }
  320.  
  321. static void RestoreMenu(void){
  322.     SetMenuBar(saveMBar);
  323.     DrawMenuBar();
  324. }
  325.  
  326.